<template>
    <div>
        <el-dialog
            :title="!form.id ? '新增' : '修改'"
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">
                <el-form ref="form" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="120px">
                    <el-form-item label="款项类型：1->收入，2->支出，3->退款，4->提现" prop="type">
                        <el-input placeholder="" v-model="form.type" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="微信支付分配的公众账号ID" prop="appId">
                        <el-input placeholder="" v-model="form.appId" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="商户号" prop="mchId">
                        <el-input placeholder="" v-model="form.mchId" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="用户标识" prop="openId">
                        <el-input placeholder="" v-model="form.openId" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="订单ID" prop="orderId">
                        <el-input placeholder="" v-model="form.orderId" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="订单编号" prop="orderSn">
                        <el-input placeholder="" v-model="form.orderSn" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="申请退款订单ID" prop="refundId">
                        <el-input placeholder="" v-model="form.refundId" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="申请退款订单编号" prop="refundSn">
                        <el-input placeholder="" v-model="form.refundSn" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="微信支付交易订单号" prop="transactionId">
                        <el-input placeholder="" v-model="form.transactionId" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="金额" prop="amount">
                        <el-input placeholder="" v-model="form.amount" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="交易内容" prop="description">
                        <el-input placeholder="" v-model="form.description" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="创建时间" prop="ctime">
                        <el-input placeholder="" v-model="form.ctime" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <!--
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio-button :label="1">正常</el-radio-button>
                            <el-radio-button :label="0">锁定</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="薪资" prop="salary">
                        <el-input-number v-model="form.salary" :precision="2" :step="0.01" :min="0.00" style="width: 80%;"></el-input-number>
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="form.sort" :min="1" :max="10000" placeholder="排序"></el-input-number>
                    </el-form-item>
                    <el-form-item label="性别" prop="sex">
                        <el-select v-model="form.sex" placeholder="请选择">
                            <el-option label="男" value="男"></el-option>
                            <el-option label="女" value="女"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="生日" prop="birthday">
                        <el-date-picker
                            v-model="form.birthday"
                            type="date"
                            placeholder="选择日期"
                            value-format="yyyy-MM-dd"
                            style="width: 63%;">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="备注" prop="remarks">
                        <el-input v-model="form.remarks" type="textarea" :rows="4" placeholder="请输入内容" style="width: 80%;"></el-input>
                    </el-form-item>
                    -->

                </el-form>
            </div>


            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            submitLoading: false,

            form: {
                type: null,
                appId: null,
                mchId: null,
                openId: null,
                orderId: null,
                orderSn: null,
                refundId: null,
                refundSn: null,
                transactionId: null,
                amount: null,
                description: null,
                ctime: null
            },
            rules: {
                type: [
                    { required: true, message: '款项类型：1->收入，2->支出，3->退款，4->提现不能为空', trigger: 'blur' }
                ],
                appId: [
                    { required: true, message: '微信支付分配的公众账号ID不能为空', trigger: 'blur' }
                ],
                mchId: [
                    { required: true, message: '商户号不能为空', trigger: 'blur' }
                ],
                openId: [
                    { required: true, message: '用户标识不能为空', trigger: 'blur' }
                ],
                orderId: [
                    { required: true, message: '订单ID不能为空', trigger: 'blur' }
                ],
                orderSn: [
                    { required: true, message: '订单编号不能为空', trigger: 'blur' }
                ],
                refundId: [
                    { required: true, message: '申请退款订单ID不能为空', trigger: 'blur' }
                ],
                refundSn: [
                    { required: true, message: '申请退款订单编号不能为空', trigger: 'blur' }
                ],
                transactionId: [
                    { required: true, message: '微信支付交易订单号不能为空', trigger: 'blur' }
                ],
                amount: [
                    { required: true, message: '金额不能为空', trigger: 'blur' }
                ],
                description: [
                    { required: true, message: '交易内容不能为空', trigger: 'blur' }
                ],
                ctime: [
                    { required: true, message: '创建时间不能为空', trigger: 'blur' }
                ],
            }
        }
    },

    methods: {
        init(id) {
            const refs = this.$refs
            // 情况表单数据
            this.form.id = id || null
            this.visible = true

            this.$nextTick(() => {
                refs['form'].resetFields()
                if(this.form.id) {
                    this.getInfo(this.form.id)
                }
            })
        },

        /**
         * 详细信息
         */
        async getInfo(id) {
            if (id) {
                const res = await this.$http({
                    url: '/mall/fmsfinance/info/' + id,
                    method: 'GET'
                })
                this.form = Object.assign({}, res.data)
            }
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            const refs = this.$refs
            this.submitLoading = true
            const valid = await refs['form'].validate().catch(() => {})
            if(valid) {
                const res = await this.$http({
                    url: `/mall/fmsfinance/${!this.form.id ? 'save' : 'update'}`,
                    method: 'POST',
                    data: this.form
                })
                if (this.ResultMessage(res)) {
                    this.$emit('refreshDataList')
                    this.submitLoading = false
                    this.visible = false
                } else {
                    this.submitLoading = false
                }
            } else {
                this.submitLoading = false
            }
        }

    }
};
</script>

<style lang="scss" scoped></style>
